﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;
using System.Text;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 景区信息类
    /// </summary>
    public class CBizPushScenic : CustBizObject
    {
        private CDmDjkPushScenic m_CDmDjkPushScenic = new CDmDjkPushScenic();
        private CDmDjkPushScenic oDmDjkPushScenic
        {
            get { return m_CDmDjkPushScenic; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizPushScenic()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkPushScenic.DJK_PUSH_SCENIC);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["SCENIC_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageRows">当前页数据条数</param>
        /// <param name="pageNum">页码</param>
        /// <param name="destionId">目的地编号</param>
        /// <param name="cityId">城市编号</param>
        /// <param name="countryId">国家编号</param>
        /// <returns>查询的数据集</returns>
        public DataSet GetScenicForPageview(int pageRows, int pageNum, string destionId, string cityId, string countryId)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("ipagerows", ParameterDirection.Input, EDbType.String, 36, pageRows));
            parmCollection.Add(new CDbParm("ipagenum", ParameterDirection.Input, EDbType.String, 36, pageNum));
            parmCollection.Add(new CDbParm("destination", ParameterDirection.Input, EDbType.String, 36, destionId));
            parmCollection.Add(new CDbParm("cityid", ParameterDirection.Input, EDbType.String, 36, cityId));
            parmCollection.Add(new CDbParm("countryid", ParameterDirection.Input, EDbType.String, 36, countryId));
            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_PUSH_SCENIC_FOR_VIEW", parmCollection);

            return ds;
        }

        public DataSet GetScenicForPageview(int pageRows, int pageNum, string destionId, string cityId, string countryId, string scenicKey)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("ipagerows", ParameterDirection.Input, EDbType.String, 36, pageRows));
            parmCollection.Add(new CDbParm("ipagenum", ParameterDirection.Input, EDbType.String, 36, pageNum));
            parmCollection.Add(new CDbParm("destination", ParameterDirection.Input, EDbType.String, 36, destionId));
            parmCollection.Add(new CDbParm("cityid", ParameterDirection.Input, EDbType.String, 36, cityId));
            parmCollection.Add(new CDbParm("countryid", ParameterDirection.Input, EDbType.String, 36, countryId));
            parmCollection.Add(new CDbParm("scenicKey", ParameterDirection.Input, EDbType.String, 255, scenicKey));
            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_PUSH_SCENIC_FOR_VIEW", parmCollection);

            return ds;
        }

        /// <summary>
        /// 获取景区信息
        /// </summary>
        /// <param name="destinId">目的地编号</param>
        /// <param name="cityId">城市编号</param>
        /// <param name="countryId">国家编号</param>
        /// <returns>查询结果</returns>
        public DataSet GetScenicDataSet(string destinId, string cityId, string countryId)
        {
            //判断地区是否全为空
            if ((string.IsNullOrEmpty(destinId) &&
                string.IsNullOrEmpty(cityId) &&
                string.IsNullOrEmpty(countryId)))
            {
                return null;
            }

            //SQL语句
            StringBuilder sbSqlString = new StringBuilder();

            sbSqlString.Append("SELECT S.SCENIC_ID AS ID,S.SCENIC_NAME AS NAME,S.SCENIC_DEFAULT_PIC AS PIC FROM DJK_PUSH_SCENIC S WHERE S.SCENIC_IS_DELETED = 0 AND S.SCENIC_IS_DISPLAY = 1 ");

            //SQL参数
            CDbParmCollection parmCollection = new CDbParmCollection();

            #region 判断地区信息
            if (!string.IsNullOrEmpty(destinId))
            {
                sbSqlString.Append("AND S.SCENIC_DISTINATION = @DESTINID ");
                parmCollection.Add(new CDbParm("DESTINID", ParameterDirection.Input, EDbType.String, 36, destinId.Replace("'", "").Replace(",", "','")));
            }

            if (!string.IsNullOrEmpty(cityId))
            {
                sbSqlString.Append("AND S.SCENIC_CITY = @CITYID ");
                parmCollection.Add(new CDbParm("CITYID", ParameterDirection.Input, EDbType.String, 36, cityId.Replace("'", "").Replace(",", "','")));
            }

            if (!string.IsNullOrEmpty(countryId))
            {
                sbSqlString.Append("AND S.SCENIC_COUNTRY = @COUNTRYID ");
                parmCollection.Add(new CDbParm("COUNTRYID", ParameterDirection.Input, EDbType.String, 36, countryId.Replace("'", "").Replace(",", "','")));
            }
            #endregion

            sbSqlString.Append("ORDER BY S.SCENIC_IS_SPECIE DESC,S.SCENIC_UPLOAD_TIME DESC");
            //执行查询
            DataSet ds = this.DataObj.QueryBySQL(sbSqlString.ToString(), parmCollection);

            return ds;
        }



        /// <summary>
        /// 根据编号查询景区信息
        /// </summary>
        /// <param name="scenicId"></param>
        /// <returns></returns>
        public DataSet GetScenicById(string scenicId)
        {
            if (string.IsNullOrEmpty(scenicId))
            {
                return null;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("SCENICID", ParameterDirection.Input, EDbType.String, 36, scenicId.Replace("'", "").Replace(",", "','")));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT T3.*,A3.COUNTRY_CITY_NME COUNTRYNAME FROM (SELECT T2.*,A2.COUNTRY_CITY_NME CITYNAME FROM (SELECT T1.*,A1.COUNTRY_CITY_NME DESTINATIONNAME FROM (SELECT S.SCENIC_ID AS ID ,S.SCENIC_NAME AS NAME ,S.SCENIC_DETAILS AS DETAILS,S.SCENIC_DISTINATION AS DISTIANTIONID ,S.SCENIC_CITY AS CITYID,S.SCENIC_COUNTRY AS COUNTRYID FROM DJK_PUSH_SCENIC S WHERE S.SCENIC_ID = @SCENICID) T1 LEFT JOIN DJK_COUNTRY_CITY A1 ON T1.DISTIANTIONID=A1.COUNTRY_CITY_ID) T2 LEFT JOIN DJK_COUNTRY_CITY A2 ON T2.CITYID=A2.COUNTRY_CITY_ID) T3 LEFT JOIN DJK_COUNTRY_CITY A3 ON T3.COUNTRYID=A3.COUNTRY_CITY_ID ", parmCollection);

            return ds;
        }

        /// <summary>
        /// 获取当前景点数据
        /// </summary>
        /// <param name="scenicId">景点id</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-22
        public DataTable GetByID(string scenicId)
        {
            if (string.IsNullOrEmpty(scenicId))
            {
                return null;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("SCENICID", ParameterDirection.Input, EDbType.String, 36, scenicId));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT * FROM DJK_PUSH_SCENIC WHERE SCENIC_ID=@SCENICID ", parmCollection);
            if (ds != null)
            {
                return ds.Tables[0];
            }
            return null;
        }

        /// <summary>
        /// 根据景区编号查询景区信息包含上一条下一条
        /// </summary>
        /// <param name="scenicId">景区编号</param>
        /// <returns>查询结果</returns>
        public DataSet GetScenicByIdWithPrevNext(string scenicId)
        {
            if (string.IsNullOrEmpty(scenicId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("scenicid", ParameterDirection.Input, EDbType.String, 36, scenicId));

            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_PREVIEW_NEXT_SCENIC_FOR_VIEW", parmCollection);

            return ds;
        }

        /// <summary>
        /// 根据条件查询资讯信息
        /// </summary>
        /// <param name="selectType"></param>
        /// <param name="selectKey"></param>
        /// <param name="destinationId"></param>
        /// <param name="cityId"></param>
        /// <param name="countryId"></param>
        /// <param name="isDel"></param>
        /// <returns></returns>
        public DataSet GetRecommendationBySP(string selectType, string selectKey, string destinationId, string cityId, string countryId, bool isDel)
        {
            if (string.IsNullOrEmpty(selectType))
            {
                return null;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("selecttype", ParameterDirection.Input, EDbType.Integer, 1, selectType));
            parmCollection.Add(new CDbParm("selectkey", ParameterDirection.Input, EDbType.String, 100, selectKey));
            parmCollection.Add(new CDbParm("destinationid", ParameterDirection.Input, EDbType.String, 36, destinationId.Replace("'", "").Replace(",", "','")));
            parmCollection.Add(new CDbParm("cityid", ParameterDirection.Input, EDbType.String, 36, cityId.Replace("'", "").Replace(",", "','")));
            parmCollection.Add(new CDbParm("countryid", ParameterDirection.Input, EDbType.String, 36, countryId.Replace("'", "").Replace(",", "','")));
            parmCollection.Add(new CDbParm("isdel", ParameterDirection.Input, EDbType.Boolean, 1, isDel));

            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_RECOMMENDATION_FOR_VIEW", parmCollection);
            return ds;
        }

        /// <summary>
        /// 发布景区信息
        /// </summary>
        /// <param name="scenicName"></param>
        /// <param name="scenicAddr"></param>
        /// <param name="scenicPrice"></param>
        /// <param name="scenicIsSpecie"></param>
        /// <param name="defaultPic"></param>
        /// <param name="desId"></param>
        /// <param name="cityId"></param>
        /// <param name="countryId"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        public string[] AddScenicInfo(string scenicName, string scenicAddr, string scenicPrice, bool scenicIsSpecie, string defaultPic, string desId, string cityId, string countryId, string scenicContent)
        {
            if (string.IsNullOrEmpty(scenicName)
                || string.IsNullOrEmpty(defaultPic))
            {
                return new string[] { "false", "景区名称或封面图片不能为空" };
            }
            if (scenicName.Length > 100)
            {
                return new string[] { "false", "景区名称需在100个字符以内" };
            }
            if (string.IsNullOrEmpty(desId)
                || string.IsNullOrEmpty(cityId)
                || string.IsNullOrEmpty(countryId))
            {
                return new string[] { "false", "地区信息有误，请检查" };
            }

            string strVpath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.strSaveUploadSysFileDirV + this.DataObj.NewID() + ".html";//存储文件的虚拟路径
            if (scenicContent != string.Empty && strVpath != string.Empty && scenicContent != "<br />")
            {
                string strFFullPath = System.Web.HttpContext.Current.Server.MapPath(strVpath);
                using (System.IO.TextWriter streamWriter = new System.IO.StreamWriter(strFFullPath))
                {
                    streamWriter.Write(scenicContent);
                }
            }
            else
            {
                return new string[] { "false", "文件信息有误，请重试！" };
            }

            DataSet ds = new CDmDjkPushScenic();
            DataRow dr = ds.Tables[0].NewRow();

            dr["SCENIC_ID"] = this.DataObj.NewID();
            dr["SCENIC_NAME"] = scenicName;
            dr["SCENIC_ADDRESS"] = scenicAddr;
            dr["SCENIC_PRICE"] = scenicPrice;
            dr["SCENIC_IS_SPECIE"] = scenicIsSpecie;
            dr["SCENIC_DEFAULT_PIC"] = defaultPic;
            dr["SCENIC_UPLOAD_TIME"] = this.dtCurrDateTime;
            dr["SCENIC_DISTINATION"] = desId;
            dr["SCENIC_CITY"] = cityId;
            dr["SCENIC_COUNTRY"] = countryId;
            dr["CONTENT_FILE_URL"] = strVpath;
            dr["SCENIC_IS_DELETED"] = false;
            dr["SCENIC_IS_DISPLAY"] = true;
            dr["SCENIC_DETAILS"] = "TEST";

            ds.Tables[0].Rows.Add(dr);
            this.Update(ds);

            return new string[] { "true", "发布成功" };
        }

        public DataSet GetAllScenic()
        {
            DataSet ds = this.DataObj.QueryBySQL(@"SELECT * FROM DJK_PUSH_SCENIC");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet GetRecommInfoByKeyVal(string keyVal, int pageIndex, int pageSize)
        {
            CDbParmCollection oparm = new CDbParmCollection();
            oparm.Add(new CDbParm("valuekey", ParameterDirection.Input, EDbType.String, 255, keyVal.Replace(",", "','").Replace("'", "")));
            oparm.Add(new CDbParm("pageindex", ParameterDirection.Input, EDbType.Integer, 2, pageIndex));
            oparm.Add(new CDbParm("pagesize", ParameterDirection.Input, EDbType.Integer, 2, pageSize));

            return this.DataObj.QueryBySP("DJK_SELECT_RECOMMDETION_FOR_ALL_VIEW", oparm);
        }

        /// <summary>
        /// 获取 景点展示页面
        /// </summary>
        /// <param name="titleName">景点名称</param>
        /// <param name="countryid">景点国家</param>
        /// <param name="cityid">城市</param>
        /// <param name="distinationid">地区</param>
        /// <param name="isSpecie">是否为经典景区</param>
        /// <param name="pageSize">每页显示数据</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-23 
        public DataSet GetRouteDatailePageList(string titleName, string countryid, string cityid, string distinationid, int isSpecie,int pageSize,int pageIndex)
        {
            CDbParmCollection oparm = new CDbParmCollection();
            oparm.Add(new CDbParm("scenicName", ParameterDirection.Input, EDbType.String, 255, titleName));
            oparm.Add(new CDbParm("countryid", ParameterDirection.Input, EDbType.String, 36, countryid));
            oparm.Add(new CDbParm("cityid", ParameterDirection.Input, EDbType.String, 36, cityid));
            oparm.Add(new CDbParm("distinationid", ParameterDirection.Input, EDbType.String, 36, distinationid));
            oparm.Add(new CDbParm("isSpecie", ParameterDirection.Input, EDbType.Integer, 2, isSpecie));

            oparm.Add(new CDbParm("pageindex", ParameterDirection.Input, EDbType.Integer, 2, pageIndex));
            oparm.Add(new CDbParm("pagesize", ParameterDirection.Input, EDbType.Integer, 2, pageSize));

            return this.DataObj.QueryBySP("DJK_SELECT_PUSH_SCENIC_LIST_VIEW", oparm);
        
        }
    }
}
